﻿$('#addProject').click(function () {
  $('#projectTitle').html('新建项目');
  $('#save').html('新增');
  /*
   PROJECT_STAGE = 项目阶段
   PROJECT_TYPE = 项目类型
   PROJECT_BRAND = 项目品牌
   */
  PROJECT_STAGE('PROJECT_STAGE, PROJECT_TYPE, PROJECT_BRAND', function (data) {
    //var arr = {
    //  PROJECT_STAGE: '项目阶段',
    //  PROJECT_TYPE: '项目类型',
    //  PROJECT_BRAND: '项目品牌'
    //};
    var arr = {};
    console.log(JSON.stringify(data));
    var obj = {};
    var list = data.ApiParamObj;
    var i = 0;
    var l = list.length;
    var key = '';

    for (; i < l; i++) {
      if (!obj[list[i].Key]) {
        obj[list[i].Key] = [];
        arr[list[i].Key] = list[i].Explain;
      }
      obj[list[i].Key].push(list[i]);
    }
    //console.log(arr)
    document.getElementById('td2').innerHTML = template('PROJECT_STAGE', obj);

    document.getElementById('td3').innerHTML = template('PROJECT_TYPE', obj);

    document.getElementById('td4').innerHTML = template('PROJECT_BRAND', obj);
  });
  $('#initProject').modal('show');
});

$('#table').on('click', '.editProject', function () {
  $('#projectTitle').html('编辑项目');
  $('#save').html('修改');
  $('#initProject').modal('show');
});

function PROJECT_STAGE(key, callback) {

  $.ajax({
    url: '/api/CommonApi?API=GetDictionarys',
    type: 'get',
    data: { Key: key, Text: '', Logogram: '' },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        callback(data);
      }
    }
  });
}


// 新增或者修改项目
$('#save').click(function () {

  var form = $('#initProject form');
  var v1 = form[0].td1.value;
  var v2 = '';
  var v3 = '';
  var v4 = '';
  if (form[0].td2.selectedIndex !== -1) {
    v2 = form[0].td2[form[0].td2.selectedIndex].value;
  }

  if (form[0].td3.selectedIndex !== -1) {
    v3 = form[0].td3[form[0].td3.selectedIndex].value;
  }

  if (form[0].td4.selectedIndex !== -1) {
    v4 = form[0].td4[form[0].td4.selectedIndex].value;
  }

  var v5 = form[0].td5.value;
  var v6 = form[0].td6.value;

  var validator = new Validator();
  validator.add(v1, [{
    strategy: 'isNonEmpty',
    errorMsg: '项目名称不能为空'
  }]);

  validator.add(v2, [{
    strategy: 'isNonEmpty',
    errorMsg: '项目阶段不能为空'
  }]);

  validator.add(v3, [{
    strategy: 'isNonEmpty',
    errorMsg: '项目类型不能为空'
  }]);

  validator.add(v4, [{
    strategy: 'isNonEmpty',
    errorMsg: '品牌不能为空'
  }]);

  validator.add(v5, [{
    strategy: 'isNonEmpty',
    errorMsg: '项目开始时间不能为空'
  }]);

  validator.add(v6, [{
    strategy: 'isNonEmpty',
    errorMsg: '项目结束时间不能为空'
  }]);



  var errmsg = validator.start();
  if (errmsg) {
    alert(errmsg);
    return false;
  }
  console.log(v1, v2, v3, v4, v5, v6)

  $.ajax({
    url: '/api/CommonApi?API=ProjectTableOp',
    type: 'put',
    data: {
      ApiParamObj: JSON.stringify({
        'Name': v1,
        'TypeID': v3,
        'BrandID': v4,
        'StageID': v2,
        'StartDate': v5,
        'EndDate': v6,
        'State': 'NO_SUBMIT'
      })
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log('success');
        $('#initProject').modal('hide');
      }
    }
  });


});